//后端 标签
import { Tag } from '@/entity/Tag';
import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import moment from 'moment';
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Essayreq ,Essayupdata} from '@/m_req/Essayreq'
import { Token } from '@/m_req/Token'
import { Like } from 'typeorm';

@Service()
export class TaghdDao {

    @Inject()
    orm!: Entity

    @Inject()
    json!: JsonResult<any>;

    

    //添加分类
    async Taghdadd(params:{tag_title:string}): Promise<res<null>> {

        let ret = 
        await this.orm.source()
            .insert()
            .into(Tag)
            .values([
                {
                    tag_title:params.tag_title
                },
            ])
            .execute()

        return this.json.success(null, msgList.add)
    }

    

    async Taghddelete(params:{tag_id:number,tag_title:string}): Promise<res<null>> {
        await this.orm.get(Tag).remove(params)
        return this.json.success(null, msgList.DeleteSuccess)
    }

    

    async Taghdupdata(params:{tag_id:number,tag_title:string}): Promise<res<null>> {
        await this.orm.get(Tag).save(params)
        return this.json.success(null, msgList.Essayupdata)
    }
    
    //分页
    async Taghdselect(params:{page:number}): Promise<res<null>> {
        let ss=Math.round((await this.orm.repository(Tag).getMany()).length/2)
        console.log("ss",ss)
        let dd= await this.orm.repository(Tag).skip((params.page*2)).take((2)).getMany()
        console.log("dd",dd)
        return this.json.success(dd, msgList.Success,700,ss)
    }

    

     //
     async Taghdqueries(params:{text:string}): Promise<res<Tag>> {
        console.log("text",params.text)
       let ret= await this.orm.get(Tag).findBy({tag_title:Like(`%${params.text}%`),})
       console.log("ret",ret)
        return this.json.success(ret, msgList.Success)
    }
    
}